﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using System.Linq.Dynamic.Core;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊排班类别
    /// </summary>
    public class OtpSchdTypeService : CrudAppService<OtpSchdType, OtpSchdTypeDto, Guid, OtpSchdTypeInput, OtpSchdTypeCreateDto, OtpSchdTypeUpdateDto>, IOtpSchdTypeService
    {
        private IRepository<OtpSchdType, Guid> _optSchdTypeRepository;

        public OtpSchdTypeService(IRepository<OtpSchdType, Guid> optSchdTypeRepository) : base(optSchdTypeRepository)
        {
            _optSchdTypeRepository = optSchdTypeRepository;
        }

        public override async Task<PagedResultDto<OtpSchdTypeDto>> GetListAsync(OtpSchdTypeInput input)
        {
            var iQuery = await _optSchdTypeRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Filter), d => d.OtpSchdTypeCodg.Contains(input.Filter) || d.OtpSchdTypeName.Contains(input.Filter));

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<OtpSchdType>, List<OtpSchdTypeDto>>(iQuery.ToList());

                return new PagedResultDto<OtpSchdTypeDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OtpSchdType>, List<OtpSchdTypeDto>>(items.ToList());

                return new PagedResultDto<OtpSchdTypeDto>(totalCount, itemsDto);
            }
        }
    }
}
